/* 简单工厂模式 */
pub struct SimplePizzaFactory {
    
}

impl SimplePizzaFactory {
    pub fn create_pizza(&self, order_type: &str, size: u32) -> Box<dyn Pizza> {
        if order_type == "greek" {
            return Box::new(GreekPizza{size});
        }
        Box::new(ChessPizza{size})
    }
}

pub trait Pizza {
    fn description(&self) ->  String;
}

pub struct ChessPizza{
    size: u32
}

impl Pizza for ChessPizza{
    fn description(&self) -> String{
        format!("这是一个 {} 寸的起司披萨", self.size)
    }
}

pub struct GreekPizza{
    size: u32
}

impl Pizza for GreekPizza {
    fn description(&self) ->  String {
        format!("这是一个 {} 寸的希腊披萨", self.size)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_simple_factory_1(){
        let factory = SimplePizzaFactory{};
        let pizza = factory.create_pizza(&String::from("chess"), 6);
        assert_eq!("这是一个 6 寸的起司披萨", pizza.description());
    }

    #[test]
    fn test_simple_factory_2() {
        let factory = SimplePizzaFactory{};
        let pizza = factory.create_pizza("greek", 8);
        assert_eq!("这是一个 8 寸的希腊披萨", pizza.description())
    }
}
